package com.sfjc.FirstTask.controller;

import com.sfjc.FirstTask.entity.User;
import com.sfjc.FirstTask.service.UserService;
import com.sfjc.FirstTask.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    //获取全部用户列表
    @GetMapping("getUserList")
    public R getUserList(){
        List<User> userList = userService.getUserList();
        log.info("info");
        return R.ok().data("item",userList);
    }

    //根据id查询用户
    @GetMapping("getUserById/{userId}")
    @Cacheable(value = "user", key = "#root.methodName")
    public R getUserById(@PathVariable Integer userId){
        User user = userService.getUserById(userId);
        return R.ok().data("user",user);
    }

    //根据id删除用户
    @DeleteMapping("deleteUserById/{userId}")
    public R deleteUserById(@PathVariable Integer userId){
        userService.deleteUserById(userId);
        return R.ok();
    }

    //根据id修改用户
    @PostMapping("updateUser")
    public R updateUser(@RequestBody User user){
        userService.updateUser(user);
        return R.ok();
    }

    //增加用户
    @PostMapping("addUser")
    public R addUser(@RequestBody User user){
        userService.addUser(user);
        return R.ok();
    }
}
